package com.pateo.bjev.bjevvehicle.ui.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.pateo.bjev.bjevvehicle.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by pateo on 17-4-25.
 */

public class TabLayout extends LinearLayout implements View.OnClickListener, ViewPager.OnPageChangeListener {

    Paint paint;
    List<OnTabSelectedListener> mListeners;
    protected int selectedIndex = 0;

    public TabLayout(Context context) {
        super(context);
        init();
    }

    public TabLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public TabLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        setWillNotDraw(false);
        setOrientation(HORIZONTAL);

        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStrokeWidth(2);
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(Color.WHITE);


    }

    public Tab newTab() {
        return new Tab();
    }

    public void addTab(Tab tab) {
        TextView textView = new TextView(getContext());
        textView.setText(tab.title);
        textView.setGravity(Gravity.CENTER);
        textView.setOnClickListener(this);
        textView.setTextSize(getResources().getDimensionPixelSize(R.dimen.tab_text_size));
        if (isFirstAdd()) {
            textView.setTextColor(getResources().getColor(R.color.tab_selected_textcolor));
        } else {
            textView.setTextColor(getResources().getColor(R.color.tab_unselected_textcolor));
        }

        LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.weight = 1;
        params.gravity = Gravity.CENTER_VERTICAL;

        addView(textView, params);
    }

    protected boolean isFirstAdd() {
        return getChildCount() == 0;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (getChildCount() > selectedIndex) {
            TextView view = (TextView) getChildAt(selectedIndex);

            int width = (int) getTextWidth(getContext(), view.getText().toString(), (int) view.getTextSize());
            int startX = (int) (view.getX() + (view.getWidth() - width) / 2);

            float startY = view.getY() + view.getHeight() + 6;
            canvas.drawLine(startX, startY, startX + width, startY, paint);
        }
    }

    private float getTextWidth(Context Context, String text, int textSize) {
        TextPaint paint = new TextPaint();
        float density = getResources().getDisplayMetrics().density;
        paint.setTextSize(density * textSize);
        return paint.measureText(text);
    }

    @Override
    public void onClick(View v) {
        if (v instanceof TextView) {
            TextView view = (TextView) v;
            int position = this.indexOfChild(view);
            selectPosition(position);
        }
    }

    public void selectPosition(int position) {
        selectedIndex = position;
        TextView selectView = (TextView) getChildAt(selectedIndex);
        selectView.setTextColor(getResources().getColor(R.color.tab_selected_textcolor));
        for (int i = 0; i < getChildCount(); i++) {
            TextView t = (TextView) getChildAt(i);
            if (i != selectedIndex) {
                t.setTextColor(getResources().getColor(R.color.tab_unselected_textcolor));
            }
        }
        postInvalidate();
        notifyTabSelectedChanged(position);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        selectPosition(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    public void addOnTabSelectedListener(OnTabSelectedListener listener) {
        if (mListeners == null) {
            mListeners = new ArrayList<>();
        }
        mListeners.add(listener);
    }

    public void removeOnTabSelectedListener(OnTabSelectedListener listener) {
        mListeners.remove(listener);
    }

    protected void notifyTabSelectedChanged(int position) {
        if (mListeners != null) {
            for (OnTabSelectedListener listener : mListeners) {
                listener.onTabSelected(position);
            }
        }

    }


    public interface OnTabSelectedListener {
        void onTabSelected(int position);
    }

    public class Tab {

        public String title;

        protected Tab() {

        }

        public Tab setText(String title) {
            this.title = title;
            return this;
        }

        public Tab setText(int res) {
            String s = TabLayout.this.getContext().getString(res);
            return setText(s);
        }
    }
}
